﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using XH.SmartParking.Base;
using XH.SmartParking.IService;
using XH.SmartParking.Entities;
using XH.SmartParking.Models;
using XH.SmartParking.Service;

namespace XH.SmartParking.ViewModels.Pages
{
    public class MenuManagementViewModel : ViewModelBase
    {
        //public DelegateCommand<string> RefreshCommand { get; set; }
        //public DelegateCommand<MenuItemModel> ModifyCommand { get; set; }
        //public DelegateCommand<MenuItemModel> DeleteCommand { get; set; }

        IMenuService _menuService;
        IDialogService _dialogService;
        IEventAggregator _eventAggregator;
        public MenuManagementViewModel(
            IRegionManager regionManager,
            IMenuService menuService,
            IDialogService dialogService,
            IEventAggregator eventAggregator)
            : base(regionManager)
        {
            PageTitle = "菜单数据维护";

            _menuService = menuService;
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;

            //RefreshCommand = new DelegateCommand<string>(Refresh);
            //ModifyCommand = new DelegateCommand<MenuItemModel>(DoModify);
            //DeleteCommand = new DelegateCommand<MenuItemModel>(DoDelete);

            Refresh();
        }

        public ObservableCollection<MenuItemModel> Menus { get; set; } =
            new ObservableCollection<MenuItemModel>();
        List<Entities.SysMenu> origMenus;

        string _searchKey;
        public override void Refresh()
        {
            Menus.Clear();
            origMenus = _menuService.GetMenuList(SearchKey).ToList();
            //FillMenus(Menus, 0);
            MenuHelper.FillMenus(Menus, null, origMenus);
        }

        public override void DoModify(object model)
        {
            DialogParameters ps = new DialogParameters();
            ps.Add("model", model);
            _dialogService.ShowDialog("ModifyMenuView", ps, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    Refresh();

                    _eventAggregator.GetEvent<RefreshMenuEvent>()
                        .Publish();
                }
            });
        }

        public override void DoDelete(object model)
        {
            try
            {
                if (MessageBox.Show("是否确定删除此项？", "提示", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    // 物理删除
                    _menuService.Delete<SysMenu>((model as MenuItemModel).MenuId);

                    // 逻辑删除
                    // 通过特定字段进行数据过滤
                    MessageBox.Show("删除完成！", "提示");

                    Refresh();

                    _eventAggregator.GetEvent<RefreshMenuEvent>()
                        .Publish();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }
    }
}
